import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//import java.util.Calendar;
//import java.util.TimerTask;

import javax.swing.*;

public class PlayVideoFrameTask implements Runnable {
	int img_width  = 320;
	int img_height = 240;
	int img_bpp    = 3;
	
	BufferedImage origImage;
	ByteBuffer buffer;
	FileInputStream fin;
	FileChannel fcin;
	JFrame frame;
	JLabel origLabel;
	
	//static long last_time = 0;
	

	/*
	 * Class constructor - initializes video 
	 */
	PlayVideoFrameTask(String in_rgb_file, int vid_fps) throws IOException {
        origImage = new BufferedImage(img_width, img_height, BufferedImage.TYPE_INT_RGB);

	    fin = new FileInputStream( in_rgb_file );
	    fcin = fin.getChannel();

	    buffer = ByteBuffer.allocateDirect( img_width * img_height * img_bpp );
	    
	    frame = new JFrame();
	    JLabel origImg = new JLabel(new ImageIcon(origImage));
	    origLabel = new JLabel("Original");
	    //JPanel panel=new JPanel();
	    
	    Container panel = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        panel.setLayout(layout);
        
         int pad =5;  
	        
	    
	    JButton playBtn =new JButton("play");
	    JButton pauseBtn =new JButton("pause");

		  
	    
	    
	    panel.add(origImg);
	    panel.add(playBtn);
	    panel.add(pauseBtn);
	    panel.add(origLabel);
	    
	    layout.putConstraint(SpringLayout.WEST, origImg ,0, SpringLayout.WEST,
	         panel);
	    layout.putConstraint(SpringLayout.NORTH, origImg , 0, SpringLayout.NORTH,
	            panel);
	    
	    layout.putConstraint(SpringLayout.SOUTH, playBtn , pad+20, SpringLayout.SOUTH,
	    		origImg);
		layout.putConstraint(SpringLayout.WEST, playBtn , 0, SpringLayout.WEST,
		            panel);
		
		layout.putConstraint(SpringLayout.SOUTH, pauseBtn , pad+20, SpringLayout.SOUTH,
	    		origImg);
		layout.putConstraint(SpringLayout.WEST, pauseBtn , 70, SpringLayout.WEST,
		            playBtn);
		
		layout.putConstraint(SpringLayout.SOUTH, origLabel , pad+20, SpringLayout.SOUTH,
	    		origImg);
		layout.putConstraint(SpringLayout.WEST, origLabel , 170, SpringLayout.WEST,
				pauseBtn);
	    
	  	    
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(img_width,img_height + 70);
        frame.setVisible(true);
        
        playBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                play_buttonMouseClicked(evt);
            }
        });
        
        pauseBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pause_buttonMouseClicked(evt);
            }
        });
        
	}
	
	int flag1=1;	int flag2=2;	//to stop redundant clicks
	
	
        private void play_buttonMouseClicked(java.awt.event.MouseEvent evt) {                                           
            
        	if(flag1==2 )
        	{
             System.out.println("play");
             flag1=1;
             flag2=2;
             Global.pause=2;

             Global.cur_vid_time=Global.paused_vid_time;
             Global.cur_aud_time=Global.paused_aud_time;
            
        	}

        } 
        
        
        private void pause_buttonMouseClicked(java.awt.event.MouseEvent evt) {                                           
            
        	if(flag2==2)
        	{
             
             flag2=1;
             flag1=2;
             Global.pause=1;
             
             Global.paused_vid_time=Global.cur_vid_time;
             Global.paused_aud_time=Global.cur_aud_time;
             
             System.out.println("paused"+" "+Global.paused_vid_time+" "+Global.cur_vid_time+" "+Global.paused_aud_time+" "+Global.cur_aud_time);
        	}
           

        } 
        
        
        
       
        	
        
        //last_time = Calendar.getInstance().getTimeInMillis();
        
	
	
	/* 
	 * Per-frame task, plays one video frame
	 * @see java.util.TimerTask#run()
	 */

	@Override
	public void run() {

        /*
         * Check for synch
         */
		long delta = Global.cur_vid_time - Global.cur_aud_time;
		//System.out.println("Time diff:" + (Global.cur_vid_time - Global.cur_aud_time));
		//System.out.println(Global.pause);
		
		if(delta > Global.upper_threshold)
		{
			Thread.yield();
			return;
		}

		buffer.clear();
		
		//long now = Calendar.getInstance().getTimeInMillis();
		
		//System.out.println(now);
		
		//long delta = last_time - now;
		
        int res = 0;
		try {
			res = fcin.read( buffer );
		} catch (IOException e) {
			e.printStackTrace();
		}

        if (res==-1) {
            //this.cancel();
        }
        
        int ind = 0;
        
        //frame.setVisible(false);
        
        for(int y = 0; y < img_height; y++)
		{
			for(int x = 0; x < img_width; x++)
			{
				short r = (short) (buffer.get(ind) & 0xff);
				short g = (short) (buffer.get(ind+img_height*img_width) & 0xff);
				short b = (short) (buffer.get(ind+img_height*img_width*2) & 0xff);
				
				int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);

				// Setting RGB here
				
				origImage.setRGB(x,y,pix);
				

				ind++;
			}
		}
        
        origLabel.setText((Global.cur_vid_time/1000000)/60 +":"+(Global.cur_vid_time/1000000)%60);
		
		//System.out.println("***"+Global.pause);
		
		
		if(Global.pause==1)
		{
			
				while(Global.pause==1)
				{
					int ctr=0;
					ctr=3;
					System.out.print(" paus");
					
				}
				 Global.cur_vid_time=Global.paused_vid_time;
	             Global.cur_aud_time=Global.paused_aud_time;
				
			
		}
		frame.repaint();
		//System.out.println(Global.cur_vid_time +" ");
		Global.cur_vid_time += 1000000/24;
				
		
	}

}
